﻿using StudentInfoSystem.Core.Models;
using StudentInfoSystem.Core.Models.Enums;
using System;
using System.Collections.Generic;
using System.Linq;

namespace StudentInfoSystem.Core.Utils
{
    public static class StudentDataGenerator
    {
        // 定义学院与专业的映射关系
        private static readonly Dictionary<string, List<string>> CollegeDepartmentMap = new()
        {
            {
                "电子信息学院", new List<string>
                {
                    "电子信息工程", "通信工程", "电子科学与技术",
                    "微电子科学与工程", "光电信息科学与工程"
                }
            },
            {
                "计算机学院", new List<string>
                {
                    "计算机科学与技术", "软件工程", "网络工程",
                    "信息安全", "人工智能", "数据科学与大数据技术"
                }
            },
            {
                "机械工程学院", new List<string>
                {
                    "机械工程", "机械设计制造及其自动化", "车辆工程",
                    "工业设计", "智能制造工程"
                }
            },
            {
                "经济管理学院", new List<string>
                {
                    "经济学", "国际经济与贸易", "工商管理",
                    "会计学", "金融学", "市场营销"
                }
            },
            {
                "外国语学院", new List<string>
                {
                    "英语", "日语", "法语", "德语", "翻译"
                }
            },
            {
                "艺术学院", new List<string>
                {
                    "音乐表演", "舞蹈学", "美术学", "设计学"
                }
            }
        };

        // 定义学院与班级前缀的映射
        private static readonly Dictionary<string, string> CollegeClassPrefixMap = new()
        {
            {"电子信息学院", "电信"},
            {"计算机学院", "计科"},
            {"机械工程学院", "机械"},
            {"经济管理学院", "经管"},
            {"外国语学院", "外语"},
            {"艺术学院", "艺术"}
        };

        // 定义生源地列表
        private static readonly List<string> Hometowns = new()
        {
            "北京", "上海", "广州", "深圳", "杭州", "南京", "武汉", "成都",
            "西安", "重庆", "天津", "苏州", "长沙", "郑州", "青岛", "宁波",
            "合肥", "福州", "厦门", "济南", "沈阳", "大连", "长春", "哈尔滨"
        };

        // 定义宿舍楼列表
        private static readonly List<string> Dormitories = new()
        {
            "学1", "学2", "学3", "学4", "学5", "学6", "学7", "学8", "学9", "学10"
        };

        private static readonly Random Random = new();

        public static List<Student> GenerateRandomStudents(int count)
        {
            var students = new List<Student>();
            var usedIds = new HashSet<string>();

            for (int i = 0; i < count; i++)
            {
                var student = new Student
                {
                    Id = GenerateUniqueId(usedIds),
                    Name = GenerateRandomName(),
                    Gender = Random.Next(2) == 0 ? Gender.Male : Gender.Female,
                    Phone = GenerateRandomPhone()
                };

                // 随机选择一个学院
                var collegeIndex = Random.Next(CollegeDepartmentMap.Count);
                var college = CollegeDepartmentMap.Keys.ElementAt(collegeIndex);
                student.College = college;

                // 根据学院选择对应的专业
                var departments = CollegeDepartmentMap[college];
                student.Department = departments[Random.Next(departments.Count)];

                // 根据学院生成班级
                student.Class = GenerateClassForCollege(college);

                // 生成其他信息
                student.Dormitory = GenerateDormitory();
                student.Hometown = Hometowns[Random.Next(Hometowns.Count)];

                students.Add(student);
            }

            return students;
        }

        private static string GenerateUniqueId(HashSet<string> usedIds)
        {
            string id;
            do
            {
                // 生成2020-2023年的学号
                var year = Random.Next(2020, 2024);
                var number = Random.Next(1, 9999).ToString("D4");
                id = $"{year}{number}";
            } while (usedIds.Contains(id));

            usedIds.Add(id);
            return id;
        }

        private static string GenerateRandomName()
        {
            var surnames = new[] { "张", "王", "李", "赵", "陈", "刘", "杨", "黄", "周", "吴" };
            var names = new[] { "伟", "芳", "娜", "秀英", "敏", "静", "磊", "洋", "艳", "勇", "军", "杰", "强", "刚", "鹏", "建", "明", "亮" };

            return surnames[Random.Next(surnames.Length)] + names[Random.Next(names.Length)];
        }

        private static string GenerateRandomPhone()
        {
            return $"1{Random.Next(3, 10)}{Random.Next(100000000, 999999999)}";
        }

        private static string GenerateClassForCollege(string college)
        {
            if (CollegeClassPrefixMap.TryGetValue(college, out var prefix))
            {
                var year = Random.Next(2020, 2024);
                var classNumber = Random.Next(1, 20);
                return $"{prefix}{year % 100}{classNumber:D2}";
            }

            return "未知班级";
        }

        private static string GenerateDormitory()
        {
            var building = Dormitories[Random.Next(Dormitories.Count)];
            var room = Random.Next(101, 999);
            return $"{building}-{room}";
        }
    }
}